import * as fgui from "fairygui-cc";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import UICore from "../../../../Core/Res/UICore";
import { Plane } from "./component/Plane";
import EventMgr from "../../../../Core/Event/EventMgr";
import { LocalUtil } from "../../../../Core/Utils/LocalUtil";
import { ModelGameRobot } from "../../model/ModelGameRobot";
import { ViewManager } from "../../base/ViewManager";
import { Grid } from "./component/Grid";
import TimerMgr from "../../../../Core/Timer/TimerMgr";
import { RandomUtils } from "../../../../Core/Utils/RandomUtils";
import { GameModels } from "../../socket/GameModels";
import WindowObject from "../../window/WindowObject";
import ModuleWindow from "../../window/ModuleWindow";
import SdkTool from "../../../../Core/Sdk/SdkTool";
import AudioManager from "../../../../Core/Audio/AudioManager";
import { MathUtils } from "../../../../Core/Utils/MathUtils";
import ConfigManager from "../../../Config/ConfigManager";
import { SkillItemRender } from "./component/SkillItemRender";
import { Tween, tween } from "cc";
import ArrayUtil from "../../../../Core/Utils/ArrayUtil";
import { Robot, TbRobot } from "../../../Config/schema/schema";

export class TeachView extends AbstractUIWindow {
    private mpboard: fgui.GComponent;
    private myboardXY = [];
    private otherboard: fgui.GComponent;
    private ctrl_state: fgui.Controller;
    private mpboardplane1: Plane;
    private mpboardplane2: Plane;
    private mpboardplane3: Plane;
    private mpboardplaneList: Array<Plane>;
    private ok: fgui.GButton;
    private random: fgui.GButton;
    private otherboardplane1: Plane;
    private otherboardplane2: Plane;
    private otherboardplane3: Plane;
    private otherboardplaneList: Array<Plane>;
    private isoverlap: boolean = false;
    private clicklist: fgui.GList;
    private tip: fgui.GTextField;
    private tip1: fgui.GTextField;
    private __time: number = 0;
    private atkgrid: Grid[] = [];
    private over: fgui.GComponent;
    private ctrl_state1: fgui.Controller;
    private again: fgui.GButton;
    private exitroom: fgui.GButton;
    // private AtkMaskGroup: fgui.GGroup;
    private AtkMask: fgui.GComponent;
    // private AtkMaskatk: fgui.GButton;
    // private AtkMaskindex: fgui.GButton;
    private AtkMaskselect: fgui.GImage;
    private mode: fgui.GButton;
    private ctrl_modestate: fgui.Controller;

    private otherPlane: Object = {};
    private touchMask: fgui.GLoader;
    private otherPlaneStr: string;
    private skillList: fgui.GList;
    private otherplaneGrid: string[];//敌方飞机的全部点位
    private myplaneGrid: string[];//我方飞机的全部点位
    private useSkill: boolean = false;
    private useItem: number = 0;
    private robotCfg: Robot;
    hedan: any;
    zhuizong: any;
    // private _guide_step: number = 0;
    private guide: fgui.GComponent;
    private guideNext: fgui.GButton;
    private ctrl_guide: fgui.Controller;

    private topbar: fgui.GComponent;
    private wofang: fgui.GComponent;
    private difang: fgui.GComponent;

    protected getResList(): Array<string> {
        return [`Plane`];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Plane", "TeachView").asCom;
    }
    protected onShow(): void {
        EventMgr.on(`planeTest`, this.planeTest, this);
        // EventMgr.on(`restart`, this.restart, this);
        EventMgr.on(`closeRobot`, this.closeRobot, this);
        EventMgr.on(`event_RespRecordData`, this.onRespRecordData, this);
        // this.AtkMaskGroup = this.AtkMask.getChild(`group`) as fgui.GGroup;
        // this.AtkMask.onClick(this.clickAtkMask, this);
        // this.AtkMaskatk = this.AtkMask.getChild(`atk`) as fgui.GButton;
        // this.AtkMaskindex = this.AtkMask.getChild(`index`) as fgui.GButton;

        this.wofang = this.topbar.getChild("wofang") as fgui.GComponent;
        this.difang = this.topbar.getChild("difang") as fgui.GComponent;
        this.tip = this.topbar.getChild("tip") as fgui.GTextField;
        this.tip1 = this.topbar.getChild("tip1") as fgui.GTextField;
        this.difang.visible = false

        this.AtkMaskselect = this.AtkMask.getChild(`select`) as fgui.GImage;
        this.hedan = this.AtkMask.getChild(`ceshi`);
        this.zhuizong = this.AtkMask.getChild(`ceshi2`);
        this.guideNext = this.guide.getChild(`next`) as fgui.GButton;
        this.ctrl_guide = this.guide.getController(`ctrl_guide`);
        this.guideNext.onClick(this.clickNext, this);
        // this.AtkMaskatk.onClick(this.clickAtkMaskatk, this);
        // this.AtkMaskindex.onClick(this.clickAtkMaskindex, this);
        this.ctrl_state1 = this.over.getController(`ctrl_state1`);
        this.again = this.over.getChild(`again`) as fgui.GButton;
        this.exitroom = this.over.getChild(`exitroom`) as fgui.GButton;
        this.over.visible = false;
        this.touchMask.onClick(this.clicktouchMask, this);
        // this.mpboard.onClick(this.click_mpboard, this);
        this.myboardXY = [this.mpboard.x, this.mpboard.y, this.mpboard.width, this.mpboard.height];
        // this.otherboard.onClick(this.click_otherboard, this);
        this.exitroom.onClick(this.clickExitRoom, this);
        this.again.onClick(this.restart, this);
        this.mpboardplane1 = this.mpboard.getChild('plane').asCom.getChild("plane1") as Plane;
        this.mpboardplane2 = this.mpboard.getChild('plane').asCom.getChild("plane2") as Plane;
        this.mpboardplane3 = this.mpboard.getChild('plane').asCom.getChild("plane3") as Plane;
        this.mpboardplaneList = [this.mpboardplane1, this.mpboardplane2, this.mpboardplane3];
        this.otherboardplane1 = this.otherboard.getChild('plane').asCom.getChild("plane1") as Plane;
        this.otherboardplane2 = this.otherboard.getChild('plane').asCom.getChild("plane2") as Plane;
        this.otherboardplane3 = this.otherboard.getChild('plane').asCom.getChild("plane3") as Plane;
        this.otherboardplaneList = [this.otherboardplane1, this.otherboardplane2, this.otherboardplane3];
        this.mode = this.otherboard.getChild(`mode`) as fgui.GButton;
        this.ctrl_modestate = this.otherboard.getController(`ctrl_state`);
        this.ctrl_modestate.onChanged(this.changemode, this);
        this.ctrl_state.onChanged(this.changestate, this);
        this.ok = this.mpboard.getChild(`ok`) as fgui.GButton;
        this.ok.onClick(this.clickOK, this);
        this.random = this.mpboard.getChild(`random`) as fgui.GButton;
        this.random.onClick(this.randomallmpboard, this);
        this.clicklist = this.otherboard.getChild(`clicklist`) as fgui.GList;
        this.clicklist.numItems = 100;
        this.clicklist.on(fgui.Event.CLICK_ITEM, this.onRender, this);
        this.pullmpboard(`5-0+0|5-6+0|0-0+0`);
        this.randomotherboard(`5-0+0|5-6+0|0-0+0`);
        this.tip.text = `准备开始游戏`;
        // TimerMgr.ins.add(1000, -1, this.countDown, this);
        (this.wofang.getChild(`headUrl`) as fgui.GLoader).url = GameModels.info.playerInfo.headUrl;
        (this.wofang.getChild(`nameTxt`) as fgui.GTextField).text = GameModels.info.playerInfo.userName;
        this.onRespRecordData();
        GameModels.lobby.send_ReqRecordData();
        (this.difang.getChild(`headUrl`) as fgui.GLoader).onClick(this.otherPlanePos, this);
        this.setRobot();
        this.skillList.itemRenderer = this.onskillList.bind(this);
        this.skillList.on(fgui.Event.CLICK_ITEM, this.clickSkillList, this);
        this.skillList.numItems = GameModels.bag.skillItem.length;

    }
    private setRobot(): void {
        this.robotCfg = RandomUtils.ins.randomArray(ConfigManager.tables.TbRobot.getDataList());
        (this.difang.getChild(`headUrl`) as fgui.GLoader).url = `robot/${this.robotCfg.avatar}.png`;
        (this.difang.getChild(`nameTxt`) as fgui.GTextField).text = this.robotCfg.nickname;
        (this.difang.getChild(`fightCount`) as fgui.GTextField).text = `战斗场次:${this.robotCfg.gameCount}`;
        (this.difang.getChild(`winRate`) as fgui.GTextField).text = `胜率:${this.robotCfg.winRate}%`;
        (this.difang.getChild(`eliminateCount`) as fgui.GTextField).text = `击落敌机:${this.robotCfg.gameKill}`;

    }
    private onRespRecordData(): void {
        (this.wofang.getChild(`fightCount`) as fgui.GTextField).text = `战斗场次:${GameModels.info.playerInfo.fightCount}`;
        (this.wofang.getChild(`winRate`) as fgui.GTextField).text = `胜率:${GameModels.info.playerInfo.winRate}%`;
        (this.wofang.getChild(`eliminateCount`) as fgui.GTextField).text = `击落敌机:${GameModels.info.playerInfo.eliminateCount}`;
    }
    private clickExitRoom(): void {
        this.close();
    }
    private changemode(): void {
        if (this.ctrl_guide.selectedIndex === 7) {
            this.ctrl_guide.selectedIndex = 8;
        }
        if (this.ctrl_guide.selectedIndex === 9) {
            this.ctrl_guide.selectedIndex = 10;
        }
        if (this.ctrl_modestate.selectedIndex == 0) {
            //作战模式
            ViewManager.ins.showtips("已切换作战模式");
            this.clicklist.touchable = true;
            for (let i of this.otherboardplaneList) {
                i.visible = false;
                i.alpha = 1;
                i.removeClick();
            }
            for (let i = 0; i < this.clicklist.numItems; i++) {
                const grid = this.clicklist.getChildAt(i) as Grid;
                if (grid.state == 8) {
                    grid.setCtrlState(0);
                }
            }
        } else {
            //蓝图模式
            ViewManager.ins.showtips("已切换蓝图模式");
            this.clicklist.touchable = false;
            for (let i of this.otherboardplaneList) {
                i.visible = true;
                i.alpha = 0.5;
                i.addClick();
            }
            for (let i = 0; i < this.clicklist.numItems; i++) {
                const grid = this.clicklist.getChildAt(i) as Grid;
                if (grid.state == 0) {
                    grid.setCtrlState(8);
                }
            }
        }
    }
    private startGame(): void {
        ModelGameRobot.state = 1;
        this.tip.text = `我的回合`;
        this.tip1.text = `20`
        this.ctrl_guide.selectedIndex = 3;
        this.ok.visible = false;
        this.skillList.numItems = GameModels.bag.skillItem.length;
        this.useSkill = false;
        this.random.visible = false;
        this.ctrl_state.selectedIndex = 1;
        this.mpboardplaneList[0].removeClick();
        this.mpboardplaneList[1].removeClick();
        this.mpboardplaneList[2].removeClick();
        this.__time = ModelGameRobot.myAtkTime;

    }
    private onRender(grid: Grid): void {
        if (this.otherboard.scaleX < 0.9) {
            // console.log(this.otherboard.scaleX, `点击列表缩放中，不处理点击事件`)
            return;
        }
        if (ModelGameRobot.state == 0) {
            // console.log(`游戏未开始，不处理点击事件`)
            return;
        }
        if (grid.state == 3 || grid.state == 4 || grid.state == 5) {
            // console.log(grid.title, `已被攻击点位，不处理点击事件`)
            return;
        }
        if (grid.state == 7) {
            grid.setCtrlState(0);
            ViewManager.ins.showtips("取消标记");
            return;
        }
        // this.AtkMask.visible = true;
        // this.AtkMaskGroup.x = ((grid.x + this.otherboard.x) - 60);
        // this.AtkMaskGroup.y = ((grid.y + this.otherboard.y) - 80);
        this.AtkMaskselect.x = grid.x + this.otherboard.x;
        this.AtkMaskselect.y = grid.y + this.otherboard.y;
        if (this.ctrl_guide.selectedIndex === 5) {
            this.ctrl_guide.selectedIndex = 6;
        }
        if (ModelGameRobot.state != 1) {
            ViewManager.ins.showtips("不是我的回合，无法攻击");
            return;
        }
        const str = grid.data as string;

        ModelGameRobot.state = 1;
        if (this.useItem == 1003) {
            this.__time = 10;
            this.useItem = 0;
            this.useItem1003(str);
        } else {
            this.__time = 3;
            GameModels.info.myatkArray.push(str);
            if (GameModels.info.myatkArray.length > 5) {
                this.ctrl_guide.selectedIndex = 11;
            }
            this.myAtkPlane(str);
        }
    }
    private clickOK(): void {
        if (ModelGameRobot.state == 0) {
            if (this.isoverlap) {
                ViewManager.ins.showtips("不能摆放重叠的飞机,已为您修正");
                this.pullmpboard(`5-0+0|5-6+0|0-0+0`);
                return;
            }
            this.startGame();
        }
    }
    private randomotherboard(str): void {
        if (!str) {
            return;
        }
        this.otherPlaneStr = str;
        this.pullotherboard(this.otherPlaneStr);
        let _count = 0;
        for (let i = 0; i < ModelGameRobot.mesh().length; i++) {
            for (let j = 0; j < ModelGameRobot.mesh()[0].length; j++) {
                const grid = (this.clicklist.getChildAt(_count++) as Grid);
                grid.title = ``;
                grid.data = `${i}-${j}`;
                grid.setCtrlState(0);
                grid.setColor();
            }
        }
    }
    private pullotherboard(str): void {
        if (!str) {
            return;
        }
        this.otherplaneGrid = [];
        const pla = str.split(`|`);
        for (let i = 0; i < pla.length; i++) {
            const pos = pla[i].split(`+`)[0];
            const ratio = pla[i].split(`+`)[1];
            const x = Number(pos.split(`-`)[0]);
            const y = Number(pos.split(`-`)[1]);
            this.otherboardplaneList[i].x = ModelGameRobot.mesh()[x][y][`x`];
            this.otherboardplaneList[i].y = ModelGameRobot.mesh()[x][y][`y`];
            this.otherboardplaneList[i].state = Number(ratio);
            this.otherboardplaneList[i].removeClick();
            this.otherboardplaneList[i].visible = false;
            this.otherPlane[i] = this.otherboardplaneList[i].gridArray;
            this.otherplaneGrid = this.otherplaneGrid.concat(this.otherboardplaneList[i].gridArray);
        }
        ArrayUtil.shuffle(this.otherplaneGrid);
    }
    private randommpboard(): void {
        const str = ModelGameRobot.randomthreeplane();
        this.pullmpboard(str);
    }
    private randomallmpboard(): void {
        this.ctrl_modestate.selectedIndex = 0;
        // this.setRobot();
        // this.randomotherboard(ModelGameRobot.randomthreeplane());
        this.randommpboard();
    }
    private pullmpboard(str): void {
        if (!str) {
            return;
        }
        this.myplaneGrid = [];
        const pla = str.split(`|`);
        for (let i = 0; i < pla.length; i++) {
            const pos = pla[i].split(`+`)[0];
            const ratio = pla[i].split(`+`)[1];
            const x = Number(pos.split(`-`)[0])
            const y = Number(pos.split(`-`)[1])
            this.mpboardplaneList[i].x = ModelGameRobot.mesh()[x][y][`x`];
            this.mpboardplaneList[i].y = ModelGameRobot.mesh()[x][y][`y`];
            this.mpboardplaneList[i].setposTarget(this.myboardXY[0], this.myboardXY[1], this.view.x, this.view.y, this.myboardXY[2], this.myboardXY[3]);
            this.mpboardplaneList[i].state = Number(ratio);
            this.myplaneGrid = this.myplaneGrid.concat(this.mpboardplaneList[i].gridArray);
            this.otherboardplaneList[i].x = ModelGameRobot.mesh()[x][y][`x`];
            this.otherboardplaneList[i].y = ModelGameRobot.mesh()[x][y][`y`];
            this.otherboardplaneList[i].setposTarget(this.myboardXY[0], this.myboardXY[1], this.view.x, this.view.y, this.myboardXY[2], this.myboardXY[3]);
            this.otherboardplaneList[i].state = Number(ratio);
        }
        ArrayUtil.shuffle(this.myplaneGrid);
    }
    private changestate(): void {
        this.otherboardplaneList[0].stopDrag();
        this.otherboardplaneList[1].stopDrag();
        this.otherboardplaneList[2].stopDrag();
        for (let i of this.otherboardplaneList) {
            // .log(`planeXY:`, i.x, i.y)
            if (i.y < 0) {
                i.x = 0;
                i.y = 0;
            }
        }
        if (this.ctrl_state.selectedIndex == 0) {
            this.wofang.visible = true
            this.difang.visible = false
            this.otherboard.touchable = false;
            this.mpboard.touchable = true;
        } else {
            this.wofang.visible = false
            this.difang.visible = true
            this.otherboard.touchable = true;
            this.mpboard.touchable = false;
        }
    }
    private clicktouchMask(): void {
        this.ctrl_state.selectedIndex = this.ctrl_state.selectedIndex == 0 ? 1 : 0;
    }
    // private click_mpboard(): void {
    //     if (this.mpboard.scaleX != 1) {
    //         if (ModelGameRobot.state == 0) {
    //             this.mpboardplaneList[0].addClick();
    //             this.mpboardplaneList[1].addClick();
    //             this.mpboardplaneList[2].addClick();
    //         }
    //         this.otherboardplaneList[0].removeClick();
    //         this.otherboardplaneList[1].removeClick();
    //         this.otherboardplaneList[2].removeClick();
    //         this.ctrl_state.selectedIndex = 0;
    //     }
    // }
    // private click_otherboard(): void {
    //     if (this.otherboard.scaleX != 1) {
    //         this.mpboardplaneList[0].removeClick();
    //         this.mpboardplaneList[1].removeClick();
    //         this.mpboardplaneList[2].removeClick();
    //         if (this.ctrl_modestate.selectedIndex == 1) {
    //             this.otherboardplaneList[0].addClick();
    //             this.otherboardplaneList[1].addClick();
    //             this.otherboardplaneList[2].addClick();
    //         }
    //         this.ctrl_state.selectedIndex = 1;
    //     }
    // }
    private planeTest(): void {
        const result = ModelGameRobot.findDuplicates(this.mpboardplane1.gridArray, this.mpboardplane2.gridArray, this.mpboardplane3.gridArray);
        this.mpboardplane1.overlap(result);
        this.mpboardplane2.overlap(result);
        this.mpboardplane3.overlap(result);
        this.isoverlap = result.length > 0;
    }
    protected onClose(): void {
        TimerMgr.ins.removeAll(this);
        Tween.stopAllByTarget(this.hedan);
        Tween.stopAllByTarget(this.zhuizong);
        Tween.stopAllByTarget(this);
        ModelGameRobot.state = 0;
        GameModels.info.myatkArray = [];
        GameModels.info.otheratkArray = [];
        GameModels.info.otherscore = 0;
        GameModels.info.myscore = 0;
        for (let i of this.atkgrid) {
            i.removeFromParent();
        }
        this.atkgrid = [];
    }
    private countDown(): void {
        switch (ModelGameRobot.state) {
            case 0:
                this.tip.text = `准备开始游戏`;
                break;
            case 1:
                //我方攻击
                if (this.__time == 0) {
                    this.hideAtkMask();
                    this.autoAtk();
                }
                this.tip.text = `我方攻击`;
                this.tip1.text = `${this.__time--}`
                break;
            case 2:
                //机器人攻击
                if (this.__time == 0) {
                    this.hideAtkMask();
                    this.__time = ModelGameRobot.myAtkTime;
                    this.ctrl_state.selectedIndex = 1;
                    if (this.ctrl_modestate.selectedIndex == 1) {
                        ViewManager.ins.showtips(`请切换到作战模式进行攻击`);
                    } else {
                        ViewManager.ins.showtips(`我方开始进攻`);
                    }
                    this.useSkill = false;
                    ModelGameRobot.state = 1;
                }
                this.tip.text = `机器人攻击`;
                this.tip1.text = `${this.__time--}`
                break;
            case 100:
                //等待动作
                if (this.__time == 0) {
                    this.robotAtk();
                }
                this.tip.text = `等待动作:`;
                this.tip1.text = `${this.__time--}`
                break;
            case 1000:
                this.tip.text = `游戏结束`;
                break;
        }
    }
    private robotAtk(): void {
        ModelGameRobot.state = 2;
        this.hideAtkMask();
        if (this.useItem == 1004 && this.useSkill) {
            this.useItem = 0;
            ViewManager.ins.showtips(`干扰弹生效,机器人无法攻击`);
            this.__time = 2;
            return;
        }
        ViewManager.ins.showtips(`机器人开始进攻`);
        // this.ctrl_state.selectedIndex = 0;
        this.__time = 15;
        TimerMgr.ins.add(10000, 1, this.robotAtkPlane, this);

    }
    private robotAtkPlane(): void {
        if (this.useItem == 1005 && this.useSkill) {
            this.useItem = 0;
            ViewManager.ins.showtips(`护盾生效,机器人攻击无效`);
            AudioManager.ins.playShot(`music/v5`);
            return;
        }
        this.hideAtkMask();
        // this.ctrl_state.selectedIndex = 0;
        let str = GameModels.info.randomotherAtk();
        const ran = RandomUtils.ins.limitInteger(0, 100);
        if (ran < this.robotCfg.ai) {
            for (let i = 0; i < this.myplaneGrid.length; i++) {
                if (GameModels.info.otheratkArray.indexOf(this.myplaneGrid[i]) == -1) {
                    str = this.myplaneGrid[i];
                    break;
                }
            }
        }
        GameModels.info.otheratkArray.push(str);
        const grid: Grid = UICore.createObject(`Plane`, `Grid`, Grid) as Grid;
        this.atkgrid.push(grid);
        this.mpboard.addChild(grid);
        const str1: string[] = str.split(`-`);
        const pos = ModelGameRobot.mesh()[Number(str1[0])][Number(str1[1])]
        grid.setPosition(pos["x"], pos[`y`]);
        for (let i = 0; i < this.mpboardplaneList.length; i++) {
            const plane = this.mpboardplaneList[i];
            for (let j = 0; j < plane.gridArray.length; j++) {
                const gridArray = plane.gridArray[j];
                if (gridArray == str) {
                    if (j == 0) {
                        grid.setCtrlState(5);
                        ViewManager.ins.showtips(`机器人攻击命中我方头部`);
                        AudioManager.ins.playShot(`music/v2`);
                        SdkTool.ins.playShock();
                        GameModels.info.otherscore += 1;
                        if (GameModels.info.otherscore == 3) {
                            this.finshGame(false);
                        }
                    } else {
                        grid.setCtrlState(4);
                        ViewManager.ins.showtips(`机器人攻击命中我方身体`);
                        AudioManager.ins.playShot(`music/v1`);
                        SdkTool.ins.playShock();
                    }
                    return;
                }
            }
        }
        grid.setCtrlState(3);
        ViewManager.ins.showtips(`机器人攻击未命中我方`);
    }
    private autoAtk(): void {
        // this.ctrl_state.selectedIndex = 1;
        ModelGameRobot.state = 100;
        const str = GameModels.info.randommyAtk();
        for (let i = 0; i < this.clicklist.numItems; i++) {
            const grid: Grid = this.clicklist.getChildAt(i);
            if (grid.data == str) {
                this.AtkMaskselect.x = grid.x + this.otherboard.x;
                this.AtkMaskselect.y = grid.y + this.otherboard.y;
                break;
            }
        }
        if (this.useItem == 1003 && this.useSkill) {
            this.__time = 10;
            this.useItem = 0;
            this.useItem1003(str);
            return;
        }
        this.__time = 3;
        GameModels.info.myatkArray.push(str);
        this.myAtkPlane(str);
    }
    private myAtkPlane(str: string | string[]): void {
        if (!(typeof str == `string`)) {
            for (let s = 0; s < str.length; s++) {
                const str1 = str[s];
                this.setOther(str1, 3);
                for (let i in this.otherPlane) {
                    const gridArray = this.otherPlane[i];
                    for (let j = 0; j < gridArray.length; j++) {
                        const grid = gridArray[j];
                        if (grid == str1) {
                            if (j == 0) {
                                this.setOther(str1, 5);
                                GameModels.info.myscore += 1;
                                if (GameModels.info.myscore == 3) {
                                    this.finshGame(true);
                                }
                            } else {
                                this.setOther(str1, 4);
                            }

                        }
                    }

                }
            }
            return;
        }

        for (let i in this.otherPlane) {
            const gridArray = this.otherPlane[i];
            for (let j = 0; j < gridArray.length; j++) {
                const grid = gridArray[j];
                if (grid == str) {
                    if (j == 0) {
                        this.setOther(str, 5);
                        // if (!this.useSkill && this.useItem != 1002) {
                        ViewManager.ins.showtips(`我方攻击命中机器人头部`);
                        // }
                        AudioManager.ins.playShot(`music/v2`);
                        SdkTool.ins.playShock();
                        GameModels.info.myscore += 1;
                        if (GameModels.info.myscore == 3) {
                            this.finshGame(true);
                        }
                    } else {
                        this.setOther(str, 4);
                        // if (!this.useSkill && this.useItem != 1002) {
                        ViewManager.ins.showtips(`我方攻击命中机器人身体`);
                        // }
                        AudioManager.ins.playShot(`music/v1`);
                        SdkTool.ins.playShock();
                    }
                    return;
                }
            }

        }
        this.setOther(str, 3);
        ViewManager.ins.showtips(`我方攻击未命中机器人`);
    }

    private setOther(str, state): void {
        for (let i = 0; i < this.clicklist.numItems; i++) {
            const grid = this.clicklist.getChildAt(i) as Grid;
            if (grid.data == str) {
                grid.setCtrlState(state);
                break;
            }
        }
    }
    private closeRobot(): void {
        this.close();
    }
    private finshGame(win: boolean): void {
        // this.ctrl_state.selectedIndex = 0;
        this.hideAtkMask();
        this.otherboardplaneList[0].visible = true;
        this.otherboardplaneList[1].visible = true;
        this.otherboardplaneList[2].visible = true;
        this.ctrl_modestate.selectedIndex = 1;
        this.over.visible = true;
        this.ctrl_state1.selectedIndex = win ? 1 : 2;
        this.otherPlanePos();
        AudioManager.ins.playShot(`music/${win ? 'win' : 'lose'}`);
        ModelGameRobot.state = 0;
        // for (let i = 0; i < this.clicklist.numItems; i++) {
        //     const grid = this.clicklist.getChildAt(i) as Grid;
        //     if (grid.state == 0) {
        //         grid.setCtrlState(6);
        //     }
        // }
    }
    private otherPlanePos(): void {
        if (!this.otherPlaneStr) {
            return;
        }
        const pla = this.otherPlaneStr.split(`|`);
        for (let i = 0; i < pla.length; i++) {
            const pos = pla[i].split(`+`)[0];
            const ratio = pla[i].split(`+`)[1];
            const x = Number(pos.split(`-`)[0]);
            const y = Number(pos.split(`-`)[1]);
            this.otherboardplaneList[i].x = ModelGameRobot.mesh()[x][y][`x`];
            this.otherboardplaneList[i].y = ModelGameRobot.mesh()[x][y][`y`];
            this.otherboardplaneList[i].state = Number(ratio);
        }
    }
    private restart(): void {
        this.randomotherboard(ModelGameRobot.randomthreeplane());
        this.over.visible = false;
        this.ctrl_modestate.selectedIndex = 0;
        this.ctrl_state.selectedIndex = 0;
        ModelGameRobot.state = 0;
        GameModels.info.myatkArray = [];
        GameModels.info.otheratkArray = [];
        GameModels.info.otherscore = 0;
        GameModels.info.myscore = 0;
        for (let i of this.atkgrid) {
            i.dispose();
        }
        this.atkgrid = [];
        const pl = LocalUtil.ins.getCookie(`plane1`);
        if (pl) {
            this.pullmpboard(pl);
        } else {
            this.randommpboard();
        }
        this.mpboardplaneList[0].addClick();
        this.mpboardplaneList[1].addClick();
        this.mpboardplaneList[2].addClick();
        this.ok.visible = true;
        this.random.visible = true;
        this.otherboardplaneList[0].visible = false;
        this.otherboardplaneList[1].visible = false;
        this.otherboardplaneList[2].visible = false;
    }
    // private clickAtkMask(): void {
    //     this.AtkMask.visible = false;
    // }
    private hideAtkMask(): void {
        this.AtkMask.visible = false;
        this.hedan.visible = false;
        this.zhuizong.visible = false;
        // if (this.AtkMaskatk.data) {
        //     this.AtkMaskatk.data = null;
        // }
        // if (this.AtkMaskindex.data) {
        //     this.AtkMaskindex.data = null;
        // }
    }
    // private clickAtkMaskatk(): void {
    //     if (this.AtkMaskatk.data) {
    //         this.AtkMaskatk.data();
    //         this.AtkMaskatk.data = null;
    //     }
    // }
    // private clickAtkMaskindex(): void {
    //     if (this.AtkMaskindex.data) {
    //         this.AtkMaskindex.data();
    //         this.AtkMaskindex.data = null;
    //     }
    // }
    private onskillList(index: number, item: SkillItemRender): void {
        item.setDate(GameModels.bag.skillItem[index], 1);
    }
    private clickSkillList(item: SkillItemRender): void {
        if (ModelGameRobot.state == 0) {
            ViewManager.ins.showtips(`游戏未开始`);
            return;
        }
        if (ModelGameRobot.state != 1) {
            ViewManager.ins.showtips(`不是我的回合，无法使用道具`);
            return;
        }
        // if (this.useSkill) {
        //     ViewManager.ins.showtips(`本回合已使用过道具`);
        //     return;
        // }
        if (item.count <= 0) {
            ViewManager.ins.showtips(`${item.vo.cfg.name}数量不足`);
            return;
        }
        this.useSkill = true;
        item.setCountTxt(0);
        switch (item.vo.cfg.id) {
            case 1002:
                //追踪导弹
                ViewManager.ins.showtips(`使用追踪导弹`);
                this.useItem1002();
                break;
            case 1003:
                //核弹
                ViewManager.ins.showtips(`使用核弹,请选择攻击位置`);
                this.useItem = 1003;
                if (this.ctrl_guide.selectedIndex === 12) {
                    this.ctrl_guide.selectedIndex = 13;
                }
                break;
            case 1004:
                //干扰弹
                ViewManager.ins.showtips(`使用干扰弹,对方下回合无法攻击`);
                AudioManager.ins.playShot(`music/v7`);
                this.useItem = 1004;
                break;
            case 1005:
                //能量护盾
                ViewManager.ins.showtips(`使用护盾,下回合不会受到伤害`);
                this.useItem = 1005;
                break;
        }

    }
    private useItem1002(): void {
        // this.ctrl_state.selectedIndex = 1;

        let str = ``;
        for (let i of this.otherplaneGrid) {
            if (GameModels.info.myatkArray.indexOf(i) == -1) {
                str = i;
                break;
            }
        }
        for (let i = 0; i < this.clicklist.numItems; i++) {
            const grid: Grid = this.clicklist.getChildAt(i);
            if (grid.data == str) {
                this.AtkMaskselect.x = grid.x + this.otherboard.x;
                this.AtkMaskselect.y = grid.y + this.otherboard.y;
                break;
            }
        }
        this.__time = 3;
        this.AtkMask.visible = true;
        this.zhuizong.visible = true;
        this.zhuizong.x = this.AtkMaskselect.x + 35;
        this.zhuizong.y = 1334;
        AudioManager.ins.playShot(`music/v6`);
        tween(this.zhuizong).to(2.0, { y: this.AtkMaskselect.y + 35 }).delay(2.0).call(() => {
            this.hideAtkMask();
            if (this.ctrl_guide.selectedIndex === 11) {
                this.ctrl_guide.selectedIndex = 12;
            }
        }, this).start();
        ModelGameRobot.state = 1;
        GameModels.info.myatkArray.push(str);
        TimerMgr.ins.add(2000, 1, () => {
            this.myAtkPlane(str);
        }, this);
    }
    private useItem1003(str: string): void {
        AudioManager.ins.playShot(`music/v3`);
        this.AtkMask.visible = true;
        this.hedan.x = this.AtkMaskselect.x + 35;
        this.hedan.y = 0;
        this.hedan.visible = true;
        tween(this.hedan).to(8.5, { y: this.AtkMaskselect.y + 70 }).delay(0.1).call(() => {
            this.hideAtkMask();
            this.ctrl_guide.selectedIndex = 14;
        }, this).start();
        TimerMgr.ins.add(8500, 1, () => {
            SdkTool.ins.playShock();
            let x = Number(str.split(`-`)[0]);
            let y = Number(str.split(`-`)[1]);
            const arr = [];
            for (let i = x - 1; i < x + 2; i++) {
                if (i < 0) {
                    continue;
                }
                for (let j = y - 1; j < y + 2; j++) {
                    if (j < 0) {
                        continue;
                    }
                    const xy = `${i}-${j}`;
                    if (GameModels.info.myatkArray.indexOf(xy) == -1) {
                        GameModels.info.myatkArray.push(xy);
                        arr.push(xy);
                    }
                }

            }
            this.myAtkPlane(arr);
        }, this);
    }
    private clickNext(): void {
        if (this.ctrl_guide.selectedIndex < 15) {
            if (this.ctrl_guide.selectedIndex === 3) {
                this.ctrl_state.selectedIndex = 1;
            }
            this.ctrl_guide.selectedIndex++;

        } else {
            this.close();
            ModuleWindow.ins.openWindow(WindowObject.ins.TeacherRewardWindow);
        }
    }
}